s_ch = []
for i in range (2, 10000):
    f = 1
    for j in range (2, i//2):
        if i%j == 0:
            f= 0
            break
    if f:
        s_ch.append(i)

nihego = 0
ch = input()
if 999 < int(ch) < 10000:
    for i0 in '', ch[0]:
        for i1 in '', ch[1]:
            for i2 in '', ch[2]:
                for i3 in '', ch[3]:
                    if i0+i1+i2+i3:
                        k = int(i0+i1+i2+i3)
                        if k in s_ch:
                            nihego = max(nihego, k)
    print(nihego)

else:
    print(0)
